<template>
    <el-container class="home-contanier">
        <el-header>
            <div>
                <img src="../../assets/img/heima.png" alt="" />
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>

        <el-container>
            <el-aside :width="collapse ? '64px': '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区 -->
                <el-menu :collapse-transition="false" :collapse="collapse"
                background-color="#333744" text-color="#fff" active-text-color="#409EFF"
                :unique-opened="true" router :default-active="activePath">
                    <!-- el-submenu:一级菜单 index要不同,如果相同,点击以下会展开相同的index字符串, -->
                    <el-submenu :index="menu.id+''" v-for="menu in menulist" :key="menu.id">
                        <!-- 一级菜单的模板区域 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconsObj[menu.id]"></i>
                            <span>{{menu.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item @click="saveNavState(child.path)" :index="`/${child.path}`"
                        v-for="child in menu.children" :key="child.id">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <span>{{child.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>

           <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
  name: 'Home',

  data () {
    return {
      menulist: [],
      iconsObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-lock',
        101: 'el-icon-shopping-bag-1',
        102: 'el-icon-notebook-2',
        145: 'el-icon-position'
      },
      collapse: false, // 是否折叠
      activePath: '' // 被激活的连接地址
    }
  },

  created () {
    this.getMenuList()

    this.activePath = window.sessionStorage.getItem('activePath')
  },

  mounted () {},

  methods: {
    // 退出登录
    logout () {
      window.sessionStorage.clear('token')

      this.$router.push('/login')
    },

    async getMenuList () {
      const { data, meta } = await this.$http.menus.getMenus()
      if (meta.status === 200) {
        this.menulist = data
      } else {
        this.$message.error('获取Menu数据错误')
      }
    },

    // 点击展开或折叠侧边栏
    toggleCollapse () {
      this.collapse = !this.collapse
    },

    // 保存点击是侧边栏的状态
    saveNavState (item) {
      window.sessionStorage.setItem('activePath', item)

      this.activePath = item
    }
  }
}
</script>

<style lang="less" scoped>
    .toggle-button {
        background-color: #4a5064;
        text-align: center;
        font-size: 10px;
        font-weight: bold;
        color: #fff;
        line-height: 24px;
        width: 200px;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        color: #fff;
        font-size: 20px;
        > div {
            display: flex;
            align-items: center;
            span {
                margin-left: 15px;
            }
        }
    }
.el-aside {
    background-color: #333744;
}
.el-main {
    background-color: #eaedf1;
}
.home-contanier {
    height: 100%;
}
</style>
